<style type="text/css">
h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
code { color: #333366; font-style: italic; }
code.opt { font-weight: bold; font-style: normal; }
.error { color: #993333; }
.warning { color: #cc33cc; }
</style>
<?php
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    require_once('admin.php');
    
    $title = 'Referrers';
    $this_file = 'lp-stats/referrers.php';
    $parent_file = 'lp-stats/page-hits.php';
    
    $submenu[$this_file][] = array(
        $title,
        6,
        $this_file,
        "LightPress $title"
        );
    
?>
<div class="wrap">

<?php
    if (!empty($message))
        echo '<div class="updated"><p><strong>' . $message . '.</strong></p></div>';
    
    //options menu
    $viewmodes = array('today', 'yesterday', 'week', 'month');
    
    //get current local time information
    $local_offset = get_option('gmt_offset');
    $local_time = time() + $local_offset*3600;
    $local_data = array('year'  => (int) gmdate('Y', $local_time),
                        'month' => (int) gmdate('m', $local_time),
                        'day'   => (int) gmdate('d', $local_time),
                        'hour'  => (int) gmdate('H', $local_time),
                        'minute'=> (int) gmdate('i', $local_time),
                        'second'=> (int) gmdate('s', $local_time));
    
    switch ( (isset($_GET['viewmode'])) ? $_GET['viewmode'] : '') {
        case 'week':
            $currentmode = 'week';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']-6);
            $end_ts = $start_ts + 7*24*3600; // end of week
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600)
                        . ' &mdash ' .
                        gmstrftime(get_option('lp_opt_short_date_format'), $end_ts + $local_offset*3600);
            break;
        case 'month':
            $currentmode = 'month';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], 1);
            $end_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month']+1, 1);
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600)
                        . ' &mdash ' .
                        gmstrftime(get_option('lp_opt_short_date_format'), $end_ts + $local_offset*3600);
            break;
        case 'yesterday':
            $currentmode = 'yesterday';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']-1);
            $end_ts = $start_ts + 24*3600; // end of day
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600);
            break;
        default:
            $currentmode = 'today';
            $start_ts = gmmktime(-1 * $local_offset, 0, 0, $local_data['month'], $local_data['day']);
            $end_ts = $start_ts + 24*3600; // end of day
            $showdate = gmstrftime(get_option('lp_opt_short_date_format'), $start_ts + $local_offset*3600);
            break;
    }
    
    echo "<h2>Top Referrers for: $showdate </h2>";
    
    foreach($viewmodes as $mode) {
        if ($mode == $currentmode)
            echo '<strong>' . ucfirst($mode) . '</strong>';
        else
            echo ' <a href="' . get_option('siteurl') . '/wp-admin/admin.php?page=' .
                $this_file . '&viewmode=' . $mode . '">' . ucfirst($mode) . '</a> ';
    }
?>

<table width="100%" cellspacing="3" cellpadding="3">
<tr>
    <th>Request URL</th>
    <th>Hits</th>
</tr>

<?php
    $datelist = '';
    $nextday = $start_ts + 24*3600;
    while ($nextday < $end_ts) {
        if (!empty($datelist))
            $datelist .= ',';
        $datelist .= "'" . gmdate('Y-m-d', $nextday) . "'";
        $nextday += 24*3600;
    }

    $qry_refs = "SELECT LEFT(CONCAT(referrer, '?', referrer_query_string), 100) as refer_short,
                        CONCAT(referrer, '?', referrer_query_string) as refer_full,
                        COUNT(*) AS hits
                   FROM {$table_prefix}stats 
                  WHERE (day='" . gmdate('Y-m-d', $start_ts) . "' AND time>='" . gmdate('H:i:s', $start_ts) . "')
                     OR (day='" . gmdate('Y-m-d', $end_ts) .   "' AND time<'" . gmdate('H:i:s', $end_ts) . "')
                " . ( (!empty($datelist)) ? " OR day IN ($datelist) " : '') . "
               GROUP BY CONCAT(referrer, '?', referrer_query_string)
               ORDER BY hits desc";
    
    $referrers = $wpdb->get_results($qry_refs);
    
    $alt = true;
    if ($referrers) {
        foreach ($referrers as $ref) {
            echo ($alt ? '<tr class="alternate">' : '<tr>') . "\n";
            echo "<td class=\"desc\"><a href=\"{$ref->refer_full}\">{$ref->refer_short}</a></td><td class=\"vers\">{$ref->hits}</td>\n";
            $alt = !$alt;
        }
    }
?>    

</table>

</div>
